﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Arthas.Controls.Metro;
using Arthas.Utility.Media;
using System.Reflection;
using System.Windows.Threading;
using System.Runtime.InteropServices;
using System.Configuration;

namespace ELM
{
    /// <summary>
    /// ChangePwd.xaml 的交互逻辑
    /// </summary>
    public partial class ChangePwd : MetroWindow
    {
        public ChangePwd()
        {
            InitializeComponent();
        }
        private void MetroButton_Click_Reset(object sender, RoutedEventArgs e)
        {
            Password.Text = "";
            Password_Copy.Text = "";
        }
       
        private void MetroButton_Click_Change(object sender, RoutedEventArgs e)
        {
            string sEmployeeId = EmployeeId.Text.Trim();
            string sPassword = Password.Text.Trim();
            string sPasswordCopy = Password_Copy.Text.Trim();

            string exite = String.Format("select count(Employee_Id) from Account where Employee_Id='{0}'", sEmployeeId);
            int isexite = DataAccess.ExecuteScalar(exite);
            //正常修改密码
            if (isexite > 0)
            {
                if (sPassword.Equals(sPasswordCopy))
                {   
                    string sqlcommand = String.Format("update Account set Pass_Word='{0}' where Employee_Id='{1}'", Md5.CreateMd5(sPassword),sEmployeeId);
                    if (DataAccess.ExcuteNonQuery(sqlcommand) > 0)
                    {
                        MessageBox.Show("非首次登录修改:修改成功");
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("两次密码输入不一");
                }
            }
            //首次登录修改密码
            else {
                if (sPassword.Equals(sPasswordCopy))
                {   //Is_Frist_Login目前没什么作用
                    string sqlcommand = String.Format("insert into Account values('{0}','{1}','{2}')", sEmployeeId, Md5.CreateMd5(sPassword), 1);
                    if (DataAccess.ExcuteNonQuery(sqlcommand) > 0)
                    {
                        MessageBox.Show("首次登录修改：修改成功请重新登陆");
                        Login lo = new Login();
                        lo.Show();
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("两次密码输入不一");
                }
            }
        }
    }
}